#include <QSqlRecord>
#include <QDebug>
#include <QMessageBox>
#include "infointerface.h"
#include "FaceObject/faceobject.h"
#include "ui_infointerface.h"

InfoInterface::InfoInterface(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::InfoInterface) {
    ui->setupUi(this);
}

InfoInterface::~InfoInterface() {
    delete ui;
}

void InfoInterface::on_query_btn_clicked() {
    if (ui->staff_rb->isChecked()) {
        tbl_model_.setTable("staff_info");
    } else if (ui->att_rb->isChecked()) {
        tbl_model_.setTable("attendance_info");
    } else {
        return;  // 未选中任何选项，直接返回
    }
    tbl_model_.select();  // 执行查询
    ui->tableView->setModel(&tbl_model_);
    ui->tableView->setEditTriggers(QAbstractItemView::AllEditTriggers);  // 允许编辑
}

void InfoInterface::on_logout_staff_btn_clicked() {
    // 根据id_inp控件的信息来注销员工信息
    QString arg_id = ui->id_inp->text();
    if (arg_id.isEmpty()) {
        return;
    }
    tbl_model_.setTable("staff_info");
    tbl_model_.setFilter(QString("staff_id = %1").arg(arg_id));
    tbl_model_.select();
    if (tbl_model_.rowCount() == 0) {
        QMessageBox::information(this, "提示", "注销失败，不存在对应当前编号的员工信息！");
        tbl_model_.setFilter("");
        return;
    }
    QSqlRecord record = tbl_model_.record(0);
    int64_t face_id = record.value(2).toLongLong();
    FaceObject::getInstance()->deleteFace(face_id);  // 注销人脸信息
    tbl_model_.removeRow(0);
    tbl_model_.submit();        // 提交更改
    tbl_model_.setFilter("");
    QMessageBox::information(this, "提示", "注销成功");
}

